package com.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.Utils.Result;
import com.blog.bean.domain.CourseVideo;
import com.blog.bean.dto.CourseVideoDTO;
import com.blog.bean.vo.CourseVideoVO;
import com.blog.mapper.CourseVideoMapper;
import com.blog.service.CourseVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 任浩
 * @version 1.0
 */

@Slf4j
@Service
public class CourseVideoServiceImpl implements CourseVideoService {

    @Autowired
    private CourseVideoMapper courseVideoMapper;

    /**
     * 新增课程视频
     * @param courseVideoDTO
     * @return
     */
    @Transactional
    @Override
    public Result add(CourseVideoDTO courseVideoDTO) {
        if(courseVideoDTO == null){
            return Result.error("课程视频参数不能为空");
        }
        if(courseVideoDTO.getUrl() == null || "".equals(courseVideoDTO.getUrl())){
            return Result.error("课程视频地址不能为空或为空字符串");
        }
        if(courseVideoDTO.getCourseId() == null){
            return Result.error("课程id不能为空");
        }
        if(courseVideoDTO.getChapterId() == null){
            return Result.error("章节id不能为空");
        }

        CourseVideo courseVideo = new CourseVideo();
        courseVideo.setName(courseVideoDTO.getName() == null ? "" : courseVideoDTO.getName());
        courseVideo.setUrl(courseVideoDTO.getUrl());
        courseVideo.setCourseId(courseVideoDTO.getCourseId());
        courseVideo.setChapterId(courseVideoDTO.getChapterId());
        courseVideoMapper.insert(courseVideo);

        return Result.success(courseVideo.getId());
    }

    @Override
    public Result<List<Map<String,Object>>> getByCourseId(Integer id) {
        if(id == null){
            return Result.error("课程id不能为空");
        }
        QueryWrapper<CourseVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id).eq("del",0);
        List<CourseVideo> courseVideos = courseVideoMapper.selectList(queryWrapper);

        List<Map<String,Object>> res = new ArrayList<>();
        //对课程根据章节进行分类
        //得到去重后的章节id
        List<Integer> distinctChapterIds = courseVideos.stream().map(data -> data.getChapterId())
                                .distinct().collect(Collectors.toList());
        log.info("去重后的章节id为: {}",id);
        distinctChapterIds.forEach(item ->{
            List<CourseVideoVO> collect = courseVideos.stream().filter(data -> data.getChapterId() == item)
                    .map(result -> new CourseVideoVO(result.getId(),result.getName(),result.getUrl()))
                    .collect(Collectors.toList());
            Map<String,Object> map = new HashMap<>();
            map.put("chapterId",item);
            map.put("children",collect);
            res.add(map);
        });

        log.info("根据课程id查询到的课程视频数据为: {}",res);

        return Result.success(res);
    }
}
